﻿using Autofac;
using Currency.Infra.Caching.Configurations;
using Currency.Infra.Caching.RedisExchange;
using Currency.Infra.Caching.RedisProvider;
using Microsoft.Extensions.Configuration;

namespace Currency.Infra.Caching
{
    public class AutofacCachingModule : Autofac.Module
    {
        private readonly ConnectionConfig _connectionConfig;

        public AutofacCachingModule(IConfigurationSection redisSection)
        {
            //Microsoft.Extensions.Configuration.Binder
            _connectionConfig = redisSection.Get<ConnectionConfig>();
        }

        public AutofacCachingModule(ConnectionConfig connectionConfig)
        {
            _connectionConfig = connectionConfig;
        }

        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterInstance(_connectionConfig);//实例注册 
            builder.RegisterType<RedisDatabase>().As<IRedisDatabase>().SingleInstance();//注册单例模式 
            builder.RegisterType<RedisCache>().As<IRedisProvider>().SingleInstance();//注册单例模式 
        }
    }
}
